const app = getApp()

Page({
  onLoad() {
    this.staticData = {
      type: 'sell'
    }
  },

  data: {
    address: '点击选择，请勾选哦~',
    isSubmit: false,
    isSucc: false
  },


  handleAddressTap() {
    wx.chooseLocation({
      success: (res) => {
        let {
          latitude,
          longitude,
          address
        } = res

        this.setData({
          address,
          latitude,
          longitude
        })
      }
    })
  },

  handleRadioChange (e) {
    this.staticData = {
      ...this.staticData,
      type: e.detail.value
    }
  },

  handleMessageTap(e) {
    this.staticData = {
      ...this.staticData,
      message: e.detail.value
    }
  },

  handleContactTap(e) {
    this.staticData = {
      ...this.staticData,
      contact: e.detail.value
    }
  },

  showInfo(title) {
    wx.showToast({
      title,
      icon: 'none',
      duration: 2000
    })   
  },

  handleSubmitTap() {
    if (this.data.address === '点击选择，请勾选哦~') {
      this.showInfo('请选择地址')
      return
    }

    if (this.staticData.message === '') {
      this.showInfo('请输入说明')
      return
    }

    if (this.staticData.contact === '') {
      this.showInfo('请输入联系方式')
      return
    }


    let data = {
      ...this.staticData,
      ...this.data
    }

    // wx.request({
    //   url: 'test.php', //仅为示例，并非真实的接口地址
    //   data,
    //   method: 'POST',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded' // 默认值
    //   },
    //   success (res) {
    //     console.log(res.data)
    //   }
    // })

    // 获取了全局的minipet对象，是一个数据库的连接
    app.minipet.add({
      data,
      success: (result) => {
        this.setData({
          isSucc: true
        })
      },
      fail: (error) => {
        this.setData({
          isSucc: false
        })
      },
      complete: () => {
        this.setData({
          isSubmit: true
        })
      }
    })
  },

  handleSuccTap() {
    wx.navigateBack()
  },

  handleCancelTap() {
    this.setData({
      isSubmit: false
    })
  }
})